package com.springapi.controller;

import com.springapi.utils.HttpServiceHutool;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/kuaidi100ocr")
public class kuaidi100ocrController {
    //快递100api接口

    /**
     * 快递100api平台利用ocr识别快递单信息接口（运单号、姓名、手机号）
     * @param imgBase64,需要将运单图片转换为Base64字符串
     * @return
     */
    @PostMapping("/ocr")
    public ResponseEntity<Map<String, Object>> kuaitongocr(@RequestParam String imgBase64) {
       // System.out.println("前台参数:"+imgBase64);
        // 创建一个Map来存储结果
        Map<String, Object> resultMap = new HashMap<>();

        //阿里云快瞳ocr接口地址
        String url = "https://ocr.kuaidi100.com/test/elec/detocr";

        try {
            //设置请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "APPCODE " + "8509178a1fd44095b5b01b108eb92e24");
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, Object> params = new HashMap<>();
            String jsonStr = "{\n" +
                    "    \"enableTilt\": false,\n" +
                    "    \"include\": [\"barcode\",\"receiver\",\"sender\"],\n" +
                    "    \"image\": \""+imgBase64+"\",\n" +
                    "    \"imageUrl\":null,\n" +
                    "    \"pdfUrl\": null\n" +
                    "}";
            params.put("param", jsonStr);
            HttpServiceHutool h = new HttpServiceHutool();
            resultMap = HttpServiceHutool.sendPostRequest(url, params, headers);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return ResponseEntity.ok(resultMap);
    }


}
